package com.sphd.beautysalon.model;


import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sphd.beautysalon.presenter.FoodInfomationPresenterImpl;
import com.sphd.beautysalon.base.RestaurantsApplication;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.interfaces.VolleyStringCallBack;
import com.sphd.beautysalon.utils.upload.MyMultipartRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by Hu on 2016/11/17
 */

public class FoodInfomationModelImpl implements FoodInfomationModel, VolleyStringCallBack, VolleyJsonObjectCallBack {
    FoodInfomationPresenterImpl foodInfomationPresenter;


    @Override
    public void sendUpdata(String url, String image, List<File> fileList, Map<String, String> map,
                           FoodInfomationPresenterImpl foodInfomationPresenter) {
        Log.e("URL===", url + "");
        this.foodInfomationPresenter = foodInfomationPresenter;
        MyMultipartRequest multipartRequest = new MyMultipartRequest(url,
                new Response.Listener() {
                    @Override
                    public void onResponse(Object response) {
                        getRes(response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                foodInfomationPresenter.sendError(error);
            }
        },image, fileList, map);

        RestaurantsApplication.getRequestQueue().add(multipartRequest);
    }

    private void getRes(String response) {
        try {
            foodInfomationPresenter.sendRes(new JSONObject(response).getInt("res"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendUpdataNoImage(String s, Map<String, String> map1, FoodInfomationPresenterImpl foodInfomationPresenter) {
        this.foodInfomationPresenter = foodInfomationPresenter;
        Log.e("S==", s);
        volleyUtil.setStringRequest(s, this, map1);
    }


    @Override
    public void onResponse(String response) {
        getRes(response);
    }

    @Override
    public void onResponse(JSONObject response) {
        Log.e("JSON==", response.toString());
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("error==", error.toString());
    }
}